package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private  UserService userService;

    @GetMapping
    public Result list(UserDto userDto){
      log.info("用户列表查询参数：{}",userDto);
      PageResult<User> pageResult = userService.list(userDto);
      return Result.success(pageResult);
   }
    @Log
    @PostMapping
    public Result add(@RequestBody User user){
      log.info("用户添加参数：{}",user);
      userService.add(user);
      return Result.success();
   }
    @Log
   @DeleteMapping("/{ids}")
    public Result delectById(@PathVariable List< Integer> ids){
        log.info("用户删除参数：{}",ids);
        userService.delectById(ids);
        return Result.success();
   }
   @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("用户查询参数：{}",id);
        User user = userService.getById(id);
        return Result.success(userService.getById(id));

   }
    @Log
   @PutMapping
    public Result update(@RequestBody User user){
      log.info("用户更新参数：{}",user);
      userService.update(user);
      return Result.success();
   }
   //查询所有用户信息
    @GetMapping("/list")
    public Result list(){
        log.info("获取所有用户信息");
        return Result.success(userService.listAll());
   }
   //根据角色标识查询用户信息
   @GetMapping("/role/{roleLabel}")
   public Result listByRoleLabel(@PathVariable String roleLabel) {
       log.info("根据角色标识查询用户：{}", roleLabel);
       List<User> users = userService.listByRoleLabel(roleLabel);
       return Result.success(users);
   }
   //根据部门查询用户信息
    @GetMapping("/dept/{deptId}")
    public Result listByDeptId(@PathVariable Integer deptId) {
        log.info("根据部门查询用户：{}", deptId);
        List<User> users = userService.listByDeptId(deptId);
        return Result.success(users);
    }


}
